var app = getApp(),
  $ = require("../../utils/httpHelper.js");
var address = require('../../utils/city.js')
var int = ''
Page({

  /**
   * 页面的初始数据
   * 当前    provinces:所有省份
   * citys选择省对应的所有市,
   * areas选择市对应的所有区
   * provinces：当前被选中的省
   * city当前被选中的市
   * areas当前被选中的区
   */
  data: {
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    sheng: '',
    shi: '',
    qu: '',
    moren: true,
    username: '',
    phone: '',
    yzm: '',
    shenfencodestr: "",
    gsname: '',
    sex: 0,
    addreinfo: '',
    areaInfo: '请选择所在地址',
    edit: false,
    fromobl: false,
    fromobl2: false,
    das: '',
    height: 0,
    shenfenjc: false,
    shenfencode: false,
    applypeople: [],
    yzmjc: false,
    yzmstatus: false,
    jishi: false,
    timestr: 60,
    isfirst: true,
    classid: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        that.setData({ height: res.windowHeight - 50 })
      }
    })
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    // 默认联动显示北京
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })

    if (!app.check_empty(options.classid)) {
      this.setData({
        classid: options.classid
      })
    }

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
      sheng: that.data.provinces[value[0]].name,
      shi: that.data.citys[value[1]].name,
      qu: that.data.areas[value[2]].name
    })
  },

  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },

  switch1Change: function (e) {
    this.setData({
      moren: e.detail.value
    })
  },

  inputuser(e) {
    this.setData({
      username: e.detail.value
    })
  },
  inputshenfen(e) {
    this.setData({
      shenfencodestr: e.detail.value
    })
  },
  //检测身份证号
  jcshenfencode(e) {
    let code = e.detail.value
    let name = this.data.username
    if (app.check_empty(name)) {
      wx.showToast({
        title: '名字不能是空!',
      })
    } else {
      let par = {
        rd_session: app.globalData.userInfo.rd_session,
        name: name,
        card_no: code
      }
      $.httpPost("check_idcard", par, (data, err) => {
        if(data.match){
          this.setData({
            shenfenjc: true,
            shenfencode: true
          })
        }else{
          this.setData({
            shenfenjc: true,
            shenfencode: false
          })
        }
        // this.setData({
        //   shenfenjc: true,
        //   shenfencode: true
        // })
      })
    }
  },
  inputgsname(e) {
    this.setData({
      gsname: e.detail.value
    })
  },
  inputphone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getyzm() {
    let that = this
    if (app.check_empty(this.data.phone)) {
      wx.showToast({
        title: '请输入手机号',
      })
    } else {
      if (app.checkPhone(this.data.phone)) {
        //60秒倒计时
        this.setData({
          jishi: true,
        })
        var inter = setInterval(function () {
          that.data.timestr--
          that.setData({
            timestr: that.data.timestr,
          })
          if (that.data.timestr < 0) {
            clearInterval(inter)
            that.setData({
              jishi: false,
              timestr: 60
            })
          }
        }, 1000)
        let par = {
          rd_session: app.globalData.userInfo.rd_session,
          phone: this.data.phone
        }
        $.httpPost("send_smsali", par, (data, err) => {

        })
      } else {
        wx.showToast({
          title: '手机格式不正确',
        })
      }

    }
  },
  inputyzm(e) {
    this.setData({
      yzm: e.detail.value
    })
  },
  //验证手机验证码
  jcyzm(e) {
    let yzm = e.detail.value
    let phone = this.data.phone
    if (app.check_empty(phone)) {
      wx.showToast({
        title: '手机号不能为空',
      })
    } else {
      if (app.checkPhone(this.data.phone)) {
        let par = {
          rd_session: app.globalData.userInfo.rd_session,
          phone: phone,
          code: yzm
        }
        $.httpPost("check_verify", par, (data, err) => {
          if (data.data.match) {
            this.setData({
              yzmjc: true,
              yzmstatus: true
            })
          } else {
            this.setData({
              yzmjc: true,
              yzmstatus: false
            })
          }

        })
      } else {
        wx.showToast({
          title: '手机号格式不正确',
        })
      }

    }
  },
  radioChange: function (e) {
    this.setData({
      sex: e.detail.value
    })
    //console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  //新增一个报名人
  xz() {
    wx.showLoading({
      title: '正在验证',
    })
    let that = this
    setTimeout(function () {
      wx.hideLoading()
      console.log(that.data.applypeople)
      if (that.data.applypeople.length > 4) {
        wx.showToast({
          title: '不能超过5人',
        })
        return
      }
      if (that.data.isfirst) {
        if (that.checkinfo()) {
          if (that.data.shenfencode) {
            if (that.data.yzmstatus) {
              let people = {}
              people.name = that.data.username
              people.cardcode = that.data.shenfencodestr
              people.sex = that.data.sex
              people.phone = that.data.phone
              people.yzm = that.data.yzm
              people.gsname = that.data.gsname
              people.addr = that.data.areaInfo
              that.data.applypeople.push(people)
              that.setData({
                applypeople: that.data.applypeople,
                username: '',
                phone: '',
                shenfencodestr: '',
                yzm: '',
                gsname: '',
                areaInfo: '请选择地址',
                shenfenjc: false,
                yzmstatus: false,
                isfirst: false
              })
              return people
            } else {
              wx.showToast({
                title: '验证码错误',
              })
            }

          } else {
            wx.showToast({
              title: '请先验证身份',
            })
          }
        } else {
          wx.showToast({
            title: '不能是空',
          })
        }
      } else {
        if (that.checkinfo2()) {
          if (that.data.shenfencode) {
            let people = {}
            people.name = that.data.username
            people.cardcode = that.data.shenfencodestr
            people.sex = that.data.sex
            that.data.applypeople.push(people)
            that.setData({
              applypeople: that.data.applypeople,
              username: '',
              shenfencodestr: '',
              shenfenjc: false,
              isfirst: false
            })
          } else {
            wx.showToast({
              title: '请先验证身份',
            })
          }
        } else {
          wx.showToast({
            title: '不能是空',
          })
        }
      }
    }, 1000)

  },
  checkinfo() {
    let name = this.data.username
    let phone = this.data.phone
    let yzm = this.data.yzm
    let gsname = this.data.gsname
    if (app.check_empty(name) || app.check_empty(phone) || app.check_empty(yzm) || app.check_empty(gsname)) {
      return false
    } else {
      return true
    }
  },
  checkinfo2() {
    let name = this.data.username
    if (app.check_empty(name)) {
      return false
    } else {
      return true
    }
  },
  tj() {
    if (this.data.applypeople.length == 0) {
      let that = this
      that.xz()
      wx.showLoading({
        title: '正在提交',
      })
      setTimeout(function(){
        wx.hideLoading()
        if (that.data.applypeople == 0){
          wx.showToast({
            title: '不能是空',
          })
          return
        }
        let par = {
          rd_session: app.globalData.userInfo.rd_session,
          data: that.data.applypeople,
          class_id: that.data.classid
        }
        $.httpPost("join_class", par, (data, err) => {
          if (data.result == 'success') {
            app.wxPayClass({
              order_no: data.data.order_no, openid: app.globalData.userInfo.openId,
              success: function () {
                wx.showToast({
                  title: '支付成功哈',
                })
                wx.switchTab({
                  url: '/pages/mine/mine',
                })
              },
              fail: function () {
                
                wx.showToast({
                  title: '支付失败了呢',
                })
              }
            })
          }else{
            wx.showToast({
              title: data.message,
            })
          }
        })
      },1000)
      
    } else {
      let param = {
        rd_session: app.globalData.userInfo.rd_session,
        data: this.data.applypeople,
        class_id: this.data.classid
      }
      $.httpPost("join_class", param, (data, err) => {
        if (data.result == 'success') {
          app.wxPayClass({
            order_no: data.data.order_no, openid: app.globalData.userInfo.openId,
            success: function (res) {
              wx.showToast({
                title: '支付成功哈',
              })
              app.wxPayClassok(res)
              wx.switchTab({
                url: '/pages/mine/mine',
              })
            },
            fail: function () {
              wx.showToast({
                title: '支付失败了呢',
              })
            }
          })
        }else{
          console.log('fail')
          wx.showToast({
            title: data.message,
          })
        }
      })
    }

  },


})
